import routes from './src/routes.js'
import VueRouter from 'vue-router'
import App from './src/App.vue'
import wxrouter from './src/api_modules/wxrouter.js'
import amapapp from './src/api_modules/amapapp.js'
import wxclear from './src/api_modules/wxclear'
import wxglobal from './src/api_modules/wxglobal.js'
import uyghurmodal from './src/api_modules/uyghurmodal.js'
import wxpayment from './src/api_modules/wxpayment.js'
import wxbookhelper from './src/api_modules/wxbookhelper.js'

var stream = weex.requireModule('stream');
var storage = weex.requireModule('storage');
var util = require('./src/barberConfig.js');


var img_url = util.serverurl.img_url;
var base_url = util.serverurl.base_url;
var fonturl = util.serverurl.font_url;
var icon_font_url = util.serverurl.icon_font_url;
var dist_url = util.serverurl.dist_url;
App.el = '#root'
App.router = routes

Vue.prototype.globalEvent = weex.requireModule('globalEvent');
Vue.prototype.gLang='2';// 1 维吾尔语   2 汉语
Vue.prototype.gUtil =util;
Vue.prototype.gImageUrl =img_url;
Vue.prototype.gFontUrl =fonturl;
Vue.prototype.gIconFontUrl =icon_font_url;
Vue.prototype.gBaseUrl =base_url;
Vue.prototype.gDistUrl =dist_url;
var tempPhone = 'Web';
if(weex.config.env.platform!=undefined){
    tempPhone = weex.config.env.platform;
}else if(weex.config.platform!=undefined){
    tempPhone = weex.config.platform;
}
Vue.prototype.gPhone = tempPhone;// 手机型号
Vue.prototype.modal = weex.requireModule('modal');
Vue.prototype.gIsLogined='false';
Vue.prototype.gName=' ';
Vue.prototype.gMobile=' ';
Vue.prototype.gAvatar=' ';
Vue.prototype.gBirthday=' ';
Vue.prototype.gGender=' ';
Vue.prototype.gToken=' ';
Vue.prototype.gMoney=' ';
Vue.prototype.gPoints=' ';
Vue.prototype.gIsWechat=0;
Vue.prototype.gOrders=' ';
Vue.prototype.gVersionCode=' ';
Vue.prototype.gUpdatedDate=' ';
Vue.prototype.gJsVersionCode=' ';
Vue.prototype.gCopyTest = ' ';
Vue.prototype.gTerminalID = ' ';
Vue.prototype.gSerialID= ' ';

if(Vue.prototype.gPhone == 'Web'){
    weex.install(wxrouter)
    weex.install(wxclear)
    weex.install(wxglobal)
    weex.install(uyghurmodal)
    weex.install(wxpayment)
    weex.install(wxbookhelper)
    weex.install(amapapp)
    // weex.install(wxavatar)
}

// 获取变量对应的值
var language = require("./src/language.js");
Vue.prototype.gT = function (key) {
    return language.lang(Vue.prototype.gLang,key)
}


Vue.prototype.gShowToast = function (text){
    var modal = weex.requireModule('uyghurmodal');
    modal.toast({
        'message': text,
        'duration': 3
    })
}

// Get 请求
// Vue.prototype.gGetData = function (url,callback) {
//     if(Vue.prototype.gPhone == 'Web'){
//         storage.setItem('prevUrl', window.location.href, event => {
//         });
//     }
//     storage.getItem('gToken', event => {
//         Vue.prototype.gToken = event.data;
//             var request_url =Vue.prototype.gBaseUrl + url;
//             var header = {
//                 'langId': Vue.prototype.gLang,
//                 'searialNumber': Vue.prototype.gSerialID,
//                 'phoneBrand': 'Meizu Pro 5',
//                 'osType': '1',
//                 'osVersion': '2.0',
//                 'Content-Type': 'application/x-www-form-urlencoded',
//                 'terminalId': Vue.prototype.gTerminalID,
//                 'Authorization': Vue.prototype.gToken
//             };

//             return stream.fetch({
//                 method: 'GET',
//                 type: 'json',
//                 url: request_url,
//                 headers: header
//             }, callback)
//     })
// }

Vue.prototype.gGetData = function (url,callback) {
    var request_url = Vue.prototype.gBaseUrl + url;
    var header = {
        'terminalId': '1',
        'serialNumber': '12345678',
        'langId': Vue.prototype.gLang
    };

    return stream.fetch({
        method: 'GET',
        type: 'json',
        url: request_url,
        headers: header
    }, callback)
}


// POST 请求
Vue.prototype.gPostData = function (url,bodys, callback) {
    if(Vue.prototype.gPhone == 'Web' && url.indexOf('login')==-1){
        storage.setItem('prevUrl', window.location.href, event => {
        });
    }
    storage.getItem('gToken', event => {
        Vue.prototype.gToken = event.data;
            // Vue.prototype.gIsLogined = event.data;
            var request_url =  Vue.prototype.gBaseUrl + url;
            var header = {
                'langId': Vue.prototype.gLang,
                'searialNumber': Vue.prototype.gSerialID,
                'phoneBrand': 'Meizu Pro 5',
                'osType': '1',
                'osVersion': '2.0',
                'Content-Type': 'application/x-www-form-urlencoded',
                'terminalId': Vue.prototype.gTerminalID,
                'Authorization': Vue.prototype.gToken
            };
            return stream.fetch({
                method: 'Post',
                type: 'json',
                url: request_url,
                body: bodys,
                headers: header
            }, callback)
    })

}

storage.getAllItem(event => {
    Vue.prototype.gLang = event.data['gLang'];
    if (event.data['gLang'] == null || event.data['gLang'] == '' ||event.data['gLang'] == 'undefined') {
        Vue.prototype.gLang  = '1';
        storage.setItem('gLang', '1', function (e) {
            var global = weex.requireModule('wxglobal');
            global.setLang('1');
        });
    }
    Vue.prototype.gToken = event.data['gToken'];
    Vue.prototype.gIsLogined = event.data['gIsLogined'];
    var global = weex.requireModule('wxglobal');
    global.getTerminalType(function (e) {
        Vue.prototype.gTerminalID = e.type
        Vue.prototype.gSerialID = e.searialnumber
        new Vue(App)
            // img_url = "local://"
    })
})

Vue.prototype.gLogout = function (){
    storage.setItem('gIsLogined', 'false', event => {
        storage.setItem('gToken', ' ' , event=> {
                    Vue.prototype.gIsLogined='false';
                    Vue.prototype.gToken = ' ';
                    if (Vue.prototype.gPhone != 'Web') {
                        var global = weex.requireModule('wxglobal');
                        global.sendEvent("RefreshLogout");
                    }

        })
    })
}



function getParams (url) {
    var name,value;
    var theRequest = new Object();
    var str=url; //取得整个地址栏
    var num=str.indexOf("?")
    str=str.substr(num+1); //取得所有参数   stringvar.substr(start [, length ]
    // console.log("strong------------------"+str)
    var arr=str.split("&"); //各个参数放到数组里
    for(var i=0;i < arr.length;i++){
        num=arr[i].indexOf("=");
        if(num>0){
            name=arr[i].substring(0,num);
            value=arr[i].substr(num+1);
            theRequest[name]=value;
        }
    }
    return theRequest;
}
if(Vue.prototype.gPhone != 'Web'){
    var conf = getParams(weex.config.bundleUrl)
    var value = conf.action;
    routes.push(value?value:'/')
}
